package com.pjj.transactionservice.AOP;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 76004
 * @apiNote
 * @date 2023/2/8 11:47
 */
@Component
@Aspect
public class TransactionAdvice {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private HttpServletRequest request;


    @Pointcut("execution(Object com.pjj.transactionservice.controller.TransactionController.*(..))")
    private void verify(){
    };

//    @Pointcut("execution(Object com.pjj.transactionservice.controller.TransactionController.updateTransaction(*))")
//    private void rabbitmq(){};


    @Around("verify()")
    public Object verifyUserIsLoad(ProceedingJoinPoint joinPoint) throws Throwable {
        Map<String,Object> map = new HashMap<>();
        String token = request.getHeader("token");
        String flag = redisTemplate.opsForValue().get(token);
        System.out.println("user token:"+token);
        System.out.println("redis token:"+flag);
        if(flag!=null){
            map = (Map<String, Object>) joinPoint.proceed();
            redisTemplate.opsForValue().set(token,"1",1, TimeUnit.HOURS);
        }
        else{
            map.put("code",200);
            map.put("msg","用户未登录");
            map.put("data",null);
        }
        return map;
    }

//    @Around("rabbitmq()")
//    public Object notice(ProceedingJoinPoint joinPoint) throws Throwable {
//        String exchangeName = "direct.doInvestigation";
//        String message = "hello, blue!";
//        rabbitTemplate.convertAndSend(exchangeName,"direct.doInvestigation",message);
//        Object res = joinPoint.proceed();
//        return res;
//    }


}
